/**
 * Copyright (c) 2012 Tomi Paananen.
 *
 * Part of Quick Gestures user interface.
 */

import QtQuick 1.1
import com.nokia.symbian 1.1

Item {
    id: progressLabel
    property alias text: labelText.text
    property bool determinate: false
    property alias value: progressBar.value
    property bool shown: false

    /**
     * Hides this label after a delay.
     */
    function hideDelayed(delay)
    {
        console.debug("ProgressLabel.qml: hideDelayed()");
        hideTimer.interval = delay;
        hideTimer.restart();
    }

    /**
     * Changes the text with an animation.
     */
    function setText(text)
    {
        textChangeAnimation.newText = text;
        textChangeAnimation.restart();
    }

    height: 60

    onShownChanged: {
        if (shown) {
            labelRect.state = "shown";
        }
        else {
            labelRect.state = "hidden";
        }
    }

    Rectangle {
        id: labelRect
        width: 0
        height: 0
        anchors.centerIn: parent
        state: "hidden"
        color: "#dd000000"
        radius: 10
        smooth: true

        Text {
            id: labelText

            anchors {
                top: parent.top
                left: determinate ? parent.left : busyIndicator.right
                right: parent.right
                bottom: parent.bottom
                margins: 10
            }

            color: platformStyle.colorNormalLight
            verticalAlignment: Text.AlignVCenter
            font.pixelSize: platformStyle.fontSizeMedium
        }

        BusyIndicator {
            id: busyIndicator
            width: height
            height: parent.height - 10

            anchors {
                left: parent.left
                verticalCenter: parent.verticalCenter
                margins: 10
            }

            visible: !determinate
            running: visible
        }

        ProgressBar {
            id: progressBar

            anchors {
                left: parent.left
                right: parent.right
                bottom: parent.bottom
                leftMargin: 5
                rightMargin: 5
            }

            visible: determinate
        }

        states: [
            State {
                name: "shown"

                PropertyChanges {
                    target: labelRect
                    width: progressLabel.width
                    height: progressLabel.height
                }
                PropertyChanges {
                    target: labelText
                    opacity: 1.0
                }
                PropertyChanges {
                    target: busyIndicator
                    opacity: 1.0
                }
                PropertyChanges {
                    target: progressBar
                    opacity: 1.0
                }
                /*PropertyChanges {
                    target: labelRect
                    visible: true
                }*/
            },
            State {
                name: "hidden"

                PropertyChanges {
                    target: labelRect
                    width: 0
                    height: 0
                }
                PropertyChanges {
                    target: labelText
                    opacity: 0
                }
                PropertyChanges {
                    target: busyIndicator
                    opacity: 0
                }
                PropertyChanges {
                    target: progressBar
                    opacity: 0
                }
                /*PropertyChanges {
                    target: labelRect
                    visible: false
                }*/
            }
        ]

        transitions: [
            Transition {
                from: "hidden"
                to: "shown"

                ParallelAnimation {
                    PropertyAnimation { properties: "visible" }
                    NumberAnimation {
                        properties: "width, height"
                        duration: 500
                        easing.type: Easing.InOutBack
                    }
                    SequentialAnimation {
                        PauseAnimation { duration: 200 }
                        NumberAnimation {
                            properties: "opacity"
                            duration: 300
                        }
                    }
                }
            },
            Transition {
                from: "shown"
                to: "hidden"

                ParallelAnimation {
                    SequentialAnimation {
                        NumberAnimation {
                            properties: "opacity"
                            duration: 300
                        }
                        PauseAnimation { duration: 200 }
                        PropertyAnimation { properties: "visible" }
                    }
                    NumberAnimation {
                        properties: "width, height"
                        duration: 500
                        easing.type: Easing.InOutBack
                    }
                }
            }
        ]
    }

    Timer {
        id: hideTimer
        interval: 2000
        repeat: false
        running: false
        onTriggered: progressLabel.shown = false;
    }

    SequentialAnimation {
        id: textChangeAnimation

        property string newText

        NumberAnimation {
            target: labelText; property: "opacity"; to: 0.0; duration: 100
            easing.type: Easing.InOutQuad
        }
        PropertyAnimation {
            target: labelText; property: "text";
            to: textChangeAnimation.newText
        }
        NumberAnimation {
            target: labelText; property: "opacity"; to: 1.0; duration: 100
            easing.type: Easing.InOutQuad
        }
    }
}
